package com.uyayo.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.cms.entity.CmsModel;
import com.uyayo.cms.mapper.CmsModelMapper;
import com.uyayo.cms.model.CmsModelCondition;
import com.uyayo.cms.service.CmsModelService;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;


@Service("cmsModelService")
public class CmsModelServiceImpl implements CmsModelService {


	private final CmsModelMapper cmsModelMapper;

	@Autowired
	public CmsModelServiceImpl(CmsModelMapper cmsModelMapper) {
		this.cmsModelMapper = cmsModelMapper;
	}


	@Override
	public CmsModel getModelById(Long id){
		return cmsModelMapper.selectByPrimaryKey(id);
	}
	

	@Override
	public List<CmsModel> selectModel(CmsModelCondition cmsModelCondition) {
		Example example = new Example(CmsModel.class);
		example.createCriteria();
		return cmsModelMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<CmsModel> selectModelByPaging(Integer pageCurrent, Integer pageSize, String sort, String order, CmsModelCondition cmsModelCondition) {

		List<CmsModel> userList;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		
		Example example = new Example(CmsModel.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(cmsModelCondition.getName())){
			criteria.andLike("name","%"+cmsModelCondition.getName()+"%");
		}
		if(cmsModelCondition.getBeginCreatedTime()!=null || cmsModelCondition.getEndCreatedTime()!=null){
			criteria.andBetween("createTime", cmsModelCondition.getBeginCreatedTime(), cmsModelCondition.getEndCreatedTime());
		}
		userList = cmsModelMapper.selectByExample(example);

		//取记录总条数
		PageInfo<CmsModel> pageInfo = new PageInfo<>(userList);
		
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, userList);
	}
	

	@Override
	public int createCmsModel(CmsModel cmsModel) {
		return cmsModelMapper.insertSelective(cmsModel);
	}

	@Override
	public CmsModel selectOne(CmsModel cmsModel) {
		return cmsModelMapper.selectOne(cmsModel);
	}

	@Override
	public int updateModel(CmsModel cmsModel) {
		return cmsModelMapper.updateByPrimaryKeySelective(cmsModel);
	}

	@Override
	public int deleteModel(Long id) {
		return cmsModelMapper.deleteByPrimaryKey(id);
	}
	
}
